package org.benjamin.lending.controller;

import javax.validation.Valid;

import org.apache.tiles.request.Request;
import org.benjamin.lending.domain.form.ResetPassword;
import org.benjamin.lending.service.TrusteeshipService;
import org.benjamin.lending.service.VerifyService;
import org.benjamin.lending.utils.CommonsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by xywen on 2017/5/22.
 */
@Controller
@RequestMapping(value = "verify")
public class VerifyController {

    @Autowired
    private TrusteeshipService trusteeshipService;
    
    @Autowired
    private VerifyService verifyService;

    @GetMapping(value = "index")
    public String index(){
        return "verify.index";
    }

    @ResponseBody
    @PostMapping("realNameAuth")
    public String realNameAuth(String realName,String idNumber){
        Assert.hasText(realName,"");
        Assert.hasText(idNumber,"");
        String username = CommonsUtils.getSessionUser();
        this.trusteeshipService.realNameAuthRequest(username,realName,idNumber);
        return null;
    }
    
    @RequestMapping(value = "doResetPassword",method = RequestMethod.POST)
    public String resetPassword(@Valid ResetPassword resetPassword,BindingResult bindingResult){
    	if(!bindingResult.hasErrors()){
    		if(resetPassword.getNewPassword().equals(resetPassword.getConfirmPassword())){
    			this.verifyService.resetPassword(resetPassword.getUserId(), resetPassword.getNewPassword());
    			return "result.success";
    		}else{
    			FieldError fieldError = new FieldError("confirmPassword", "confirmPassword", "您输入的确认密码不正确");
    			bindingResult.addError(fieldError);
    		}
    		
    	}
    	return "result.error";
    }
}
